package com.youlai.boot.system.controller;

import com.youlai.boot.system.service.ReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.time.LocalDate;

/**
 * 生成报告
 */
@Tag(name = "报告管理")
@RestController
@RequestMapping("/api/v1/reports")
@RequiredArgsConstructor
public class ReportController {

    private final ReportService reportService;

    @Operation(summary = "生成PDF格式报告")
    @GetMapping("/pdf")
    public ResponseEntity<byte[]> generatePdfReport(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        reportService.generatePdfReport(startDate, endDate, outputStream);

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_PDF)
                .body(outputStream.toByteArray());
    }
}